<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Role extends Controller
{

    protected $beforeActionList = [
        'init' => ['except'=>'save,change'],
    ];


    public function init()
    {
        $request= Request::instance();
        $a=$request->controller();
        $b=$request->action();

        $pid=Db::table('hb_permission')
            ->field('id')
            ->where('controller',$a)
            ->where('method',$b)
            ->find();
//dump($pid);
        $a=Session::get('pid');
//        dump($a);
        if (!in_array($pid['id'],$a))
        {
            return $this->error('没有此权限,沙雕!!');
        }

    }

    public function index()
    {
        //        查询所有的角色信息
        $rolelist = Db::table('hb_role')
            ->field(['id', 'rolename'])
            ->select();
//        dump($rolelist);

        $permissionlist = Db::table(['hb_permission' => 'p', 'role_permission' => 'rp', 'hb_role' => 'r'])
            ->field(['p.permission', 'rp.r_id'])
            ->where('p.id=rp.p_id and r.id=rp.r_id')
            ->select();
//        dump($permissionlist);

        $arr = [];
        foreach ($rolelist as $k => $v) {
            foreach ($permissionlist as $key => $val) {
                if ($v['id'] == $val['r_id']) {
                    $v['permission'][] = $val['permission'];
                }
//                拼凑出需要的数组
                $arr[$k] = $v;
            }
        }
//            dump($arr);

        return view('admin@role/index', [
            'title'=>'角色列表',
            'data' => $arr
        ]);

    }



        /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //        return '添加表单';
        return view('role/create', [
            'title'=>'填个角色!'
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $p=$request->post();
//        dump($p);
        $id = Db::name('role')
            ->insertGetId($p);
        $a=[
            'r_id'=>$id,
            'p_id'=>1,
        ];

        $result=Db::table('role_permission')
            ->insert($a);
        if ($result>0) {
            return $this->success('添加成功', url('admin/role/index'));
        } else {
            return $this->error('添加失败,请重试!!');
        }
    }

    /**
     * 角色分配权限
     *
     * @param  string 角色id数据
     *
     * */
    public function portion($id)
    {
        //        查询所有的权限
        $permission= Db::name('permission')
            ->select();
//        dump($permission);

        //       查询当前角色对应的权限
        $result=Db::table(['hb_permission'=>'p','role_permission'=>'rp'])
            ->field('rp.p_id')
            ->where('p.id=rp.p_id')
            ->where('rp.r_id',$id)
            ->select();
//        dump($result);
//        $a=[];
//        foreach ($result as $v)
//        {
//            $a[]=$v['p_id'];
//        }
//
//        $str=implode(',',$a);
//        echo $str;


        return view('role/portion', [
            'title'=>'分配权限!',
            'permission'=>$permission,
            'result'=>$result,
            'id'=>$id,
        ]);





    }



    public function change(Request $request)
    {
        $p = $request->post();

//        dump($p);

        $a=$p['permission'];
        foreach ($a as $v)
        {
            $data[]=['r_id'=>$p['r_id'],'p_id'=>$v];
        }
        Db::table('role_permission')
            ->where('r_id',$p['r_id'])
            ->delete();
        $result= Db::table('role_permission')->insertAll($data);
        if ($result>0) {
            return $this->success('分配权限成功', url('admin/role/index'));
        } else {
            return $this->error('分配权限失败,请重试!!');
        }





    }


}
